import java.util.Iterator;

public class MyLinkedLiseTest{
    public static void main(String[] args) {
        MyLinkedLise<Integer> mylist = new MyLinkedLise<>(0);
        mylist.add(1);
        mylist.add(2);
        mylist.add(3);
        mylist.add(4);
        System.out.println(mylist);
        mylist.add(3,10);
        System.out.println(mylist);
        System.out.println(mylist.get(2));
        mylist.remove(2);
        System.out.println(mylist);
    }
}

class MyLinkedLise<E> {
    Node<E> head;
    public MyLinkedLise() {
        head = new Node<E>();
        head.next = head.previous = head;
    }
    public MyLinkedLise(E e) {
        head = new Node<E>();
        head.element = e;
        head.next = head.previous = head;
    }
    public Node<E> find(int index){
        Node<E> tmp = head;
        for(int i = 0; i < index - 1; i++)
        {
            tmp = tmp.next;
        }
        return tmp;
    }
    public void add(E e){
        Node<E> tail = head.previous;
        Node<E> tmp = new Node<>(tail,e,head);
        tail.next = tmp;
        head.previous = tmp;
    }
    public void add(int index,E e){
        Node<E> tmp = find(index);
        Node<E> next = tmp.next;
        Node<E> Newnode = new Node<>(tmp,e,next);
        tmp.next = Newnode;
        next.previous = Newnode;
    }
    public E get(int index){
        Node<E> tmp = find(index);
        return tmp.element;
    }
    public void remove(int index){
        Node<E> tmp = find(index);
        tmp.previous.next = tmp.next;
    }
    public String toString(){
        StringBuffer string = new StringBuffer();
        Node<E> tmp = head;
        while(tmp != head.previous)
        {
            string.append(tmp.element);
            string.append(" ");
            tmp = tmp.next;
        }
        string.append(tmp.element);
        return string.toString();
    }
}

class Node<E>{
    Node<E> previous;
    E element;
    Node<E> next;
    public Node(){

    }
    public Node(Node<E> previous,E element,Node<E> next){
        this.previous = previous;
        this.element = element;
        this.next = next;
    }
}
